﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MixNP.BusinessLayer
{
    public static class Categories
    {
        public static DataTable GetRootCategories()
        {
            return MixNP.DatabaseLayer.Categories.GetRootCategories();
        }

        public static DataTable GetCategories(int parentCategoryId)
        {
            return MixNP.DatabaseLayer.Categories.GetCategories(parentCategoryId);
        }

        public static bool AllowEntry(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.AllowEntry(categoryId);
        }

        public static string GetCategoryAlias(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.GetCategoryAlias(categoryId);
        }

        public static string GetCategoryName(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.GetCategoryName(categoryId);
        }

        public static int GetCategoryId(string alias)
        {
            return MixNP.DatabaseLayer.Categories.GetCategoryId(alias);
        }

        public static int GetParentCategoryId(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.GetParentCategoryId(categoryId);
        }
        
        public static DataTable GetCustomFields(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.GetCustomFields(categoryId);
        }

        public static string GetCustomFieldValue(double itemId, int customFieldId)
        {
            return MixNP.DatabaseLayer.Categories.GetCustomFieldValue(itemId, customFieldId);
        }
        public static bool IsRootCategory(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.IsRootCategory(categoryId);
        }

        public static bool IsParentCategory(int categoryId, int parentCategoryId)
        {
            return MixNP.DatabaseLayer.Categories.IsParentCategory(categoryId, parentCategoryId);
        }

        public static DataTable GetCategoryAliasView(int categoryId)
        {
            return MixNP.DatabaseLayer.Categories.GetCategoryAliasView(categoryId);
        }

        public static string GetCategoryUrl(int categoryId, System.Web.UI.Page page)
        {
            using (DataTable table = GetCategoryAliasView(categoryId))
            {
                if (table.Rows.Count.Equals(1))
                {
                    System.Data.DataRow row = table.Rows[0];
                    string categoryAlias = row["CategoryAlias"].ToString();
                    string parentCategoryAlias = row["ParentCategoryAlias"].ToString();
                    return "http://" + page.Request.Url.Authority + page.ResolveUrl("~/view/" + parentCategoryAlias + "/" + categoryAlias + ".mix");
                }
            }

            return string.Empty;
        }
    }
}
